package com.gxsyh.nantie.statements.management.provider.service.impl;

import com.gxsyh.nantie.statements.management.provider.domain.TbDepartment;
import com.gxsyh.nantie.statements.management.provider.domain.TbUser;
import com.gxsyh.nantie.statements.management.provider.domain.UserOrderCount;
import com.gxsyh.nantie.statements.management.provider.domain.UserOrderInfo;
import com.gxsyh.nantie.statements.management.provider.service.TbDepartmentService;
import com.gxsyh.nantie.statements.management.provider.service.TbUserService;
import com.gxsyh.nantie.statements.management.provider.service.UserOrderCountService;
import com.gxsyh.nantie.statements.management.provider.service.UserOrderInfoService;
import com.gxsyh.nantie.statements.management.provider.util.RailwayOrderContUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserOrderCountServiceImpl implements UserOrderCountService {

    @Resource
    private UserOrderInfoService userOrderInfoService;

    @Resource
    private TbUserService tbUserService;

    @Resource
    private TbDepartmentService tbDepartmentService;

    @Override
    public List<UserOrderCount> getClassStatement(Long classId) {
        List<UserOrderCount> userOrderCountList=new ArrayList<>();
        List<TbUser> tbUserList=tbUserService.findTeamIdGetUser(classId);
        RailwayOrderContUtil orderContUtil=new RailwayOrderContUtil();
        for (TbUser list:tbUserList)
        {
            List<UserOrderInfo> userOrderInfos=userOrderInfoService.selectUserOrderInfo(list.getId());
            UserOrderCount userOrderCount=orderContUtil.countUserOrder(userOrderInfos);
            userOrderCount.setName(list.getName());
            userOrderCount.setUserId(list.getId());
            userOrderCountList.add(userOrderCount);
        }
        return userOrderCountList;
    }

    @Override
    public List<UserOrderCount> getTeamStatement(Long teamId) {
        List<TbDepartment> tbDepartments =tbDepartmentService.selectDepartmentSubordinate(0,teamId);
        List<UserOrderCount> userOrderCountList=new ArrayList<>();
        if (tbDepartments==null)
        {
            return getClassStatement(teamId);
        }else{
            for (TbDepartment list:tbDepartments)
            {
                List<UserOrderCount> userOrderCounts=getClassStatement(list.getId());
                for (UserOrderCount list2:userOrderCounts)
                {
                    userOrderCountList.add(list2);
                }
            }
        }
        return userOrderCountList;
    }

    @Override
    public List<UserOrderCount> getWorkshopStatement(Long departmentId) {
        List<TbDepartment> tbDepartments=tbDepartmentService.selectDepartmentSubordinate(0,departmentId);
        List<UserOrderCount> userOrderCountList=new ArrayList<>();
        if (tbDepartments==null)
        {
            userOrderCountList =getTeamStatement(departmentId);
        }else{
            for (TbDepartment list:tbDepartments)
            {
                List<UserOrderCount> userOrderCounts=getTeamStatement(list.getId());
                for (UserOrderCount list2:userOrderCounts)
                {
                    userOrderCountList.add(list2);
                }
            }
        }
        return userOrderCountList;
    }

    @Override
    public List<UserOrderCount> getVehicleSegmentStatement(Long departmentId) {
        List<TbDepartment> tbDepartments=tbDepartmentService.selectDepartmentSubordinate(0,departmentId);
        List<UserOrderCount> userOrderCountList=new ArrayList<>();
        if (tbDepartments==null)
        {
            userOrderCountList =getWorkshopStatement(departmentId);
        }else{
            for (TbDepartment list:tbDepartments)
            {
                List<UserOrderCount> userOrderCounts=getWorkshopStatement(list.getId());
                for (UserOrderCount list2:userOrderCounts)
                {
                    userOrderCountList.add(list2);
                }
            }
        }
        return userOrderCountList;
    }
}
